home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / emacs / emacs1857 / src_d2.zoo / source / lib+ / sbrk.c < prev   
Encoding:
C/C++ Source or Header  |  1991-12-02  |  1.8 KB  |  85 lines

  1. /* sbrk: emulate Unix sbrk call */
  2. /* by ERS */
  3. /* jrb: added support for allocation from _heapbase when _stksize == -1 
  4.     thanks to Piet van Oostrum & Atze Dijkstra for this idea and
  5.         their diffs. */
  6.  
  7. /* WARNING: sbrk may not allocate space in continguous memory, i.e.
  8.    two calls to sbrk may not return consecutive memory. This is
  9.    unlike Unix.
  10. */
  11.  
  12. #include <osbind.h>
  13. #include <unixlib.h>
  14.  
  15.  
  16. extern void *_heapbase;
  17. extern long _stksize;
  18. static void *top_of_heap = (void *) 0;
  19.  
  20. /* Number of bytes of writable memory we can expect to be able to get */
  21. long    __lim_data = 0;
  22. long    __malloc_bytes_in_free_list = 0;
  23. long    malloc_sbrk_used = 0;
  24. long    malloc_sbrk_unused = 1024000;    /* only dummy initializer */
  25.  
  26. static void * HeapAlloc( sz )
  27. size_t sz ;
  28. {
  29.     char slush [64];
  30.     register void *sp;
  31.     long available;
  32.     
  33.     if(!top_of_heap) top_of_heap = (void *) slush;
  34.     if((_heapbase < (void *) slush) && ((void *) slush < top_of_heap))
  35.         sp = (void *) slush;
  36.     else
  37.         sp = top_of_heap;
  38.     available = (long) (sp - ((size_t)_heapbase + sz));
  39.     if (!__lim_data) __lim_data = available;
  40.     if ( available < 0 )
  41.     {
  42.     return (void *)NULL;
  43.     }
  44.     malloc_sbrk_unused = available + __malloc_bytes_in_free_list;
  45.     __malloc_bytes_in_free_list += sz;
  46.     sp = _heapbase;
  47.     _heapbase = (void *)((size_t)_heapbase + sz);
  48.     _stksize -= (long)sz;
  49.     
  50.     return( sp );
  51. }
  52.  
  53. asm(".text; .even; .globl _sbrk; _sbrk:"); /* dept of dirty tricks */
  54.  
  55. /* provided for compilers with sizeof(int) == 2 */
  56. void *lsbrk(long n)
  57. {
  58.   void *rval;
  59.  
  60.   if(_heapbase != NULL)
  61.   {
  62.       return (rval = HeapAlloc(n)) ? rval : (void *) -1L;
  63.   }
  64.   else
  65.   {
  66.     return (rval = (void *) Malloc(n)) ? rval : (void *)-1L;
  67.   }
  68. }
  69.  
  70. void brk(void *b)
  71. {
  72.   if(_heapbase != NULL && b != NULL)
  73.   {
  74.       _heapbase = b;
  75.   }
  76. }
  77.  
  78. #if 0
  79. void *sbrk(x)
  80. size_t x;
  81. {
  82.     return (void *) Malloc((long) x);
  83. }
  84. #endif
  85.